package org.example.TankGan;

import lombok.Data;

import java.io.*;

/**
 * @author cc
 * @date 2021/4/26 17:41
 * @Description 记录相关信息，并把它写出到io流
 */
@Data
public class Recorder {
    //记录我方击毁敌人数量
    private static int destroyEnemyTankNum=0;
    //io文件对象写入流
    private static FileWriter fw=null;
    //io对象缓冲流
    private static BufferedWriter bw =null;
    //io对象读取流
    private static  BufferedReader br=null;
    private static  String recordFile = "D:/java/workshop/TankWar/temp/MyRecorder.txt";

    public static int getDestroyEnemyTankNum() {
        return destroyEnemyTankNum;
    }

    public static void setDestroyEnemyTankNum(int destroyEnemyTankNum) {
        Recorder.destroyEnemyTankNum = destroyEnemyTankNum;
    }

    //击毁敌人添加
    public static  void AddAllEnemy(){
        destroyEnemyTankNum++;
    }

    public static void save(){
        try {
             bw = new BufferedWriter(new FileWriter(recordFile));
             bw.write(getDestroyEnemyTankNum()+"\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void read(){
        try {
            br = new BufferedReader(new FileReader(recordFile));
            String s = br.readLine();
            int i = Integer.parseInt(s);
            setDestroyEnemyTankNum(i);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
